package com.example.hidesms.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.example.hidesms.R;
import com.example.hidesms.HomePageFragment.HomeListener;
import com.example.hidesms.R.drawable;
import com.example.hidesms.R.id;
import com.example.hidesms.R.layout;
import com.example.hidesms.dto.CoolTitleItem;
import com.example.hidesms.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class CoolTitleAdapter extends ArrayAdapter<CoolTitleItem>{
	
	private Context mContext;
	private ArrayList<CoolTitleItem> list;
	private LayoutInflater mInflater;
	private int contactSize, width;
	private RelativeLayout.LayoutParams imgParams;
	private HomeListener homeListener;
	
	public void setHomeListener(HomeListener homeListener) {
		this.homeListener = homeListener;
	}

	
	public CoolTitleAdapter(Context context, int textViewResourceId, ArrayList<CoolTitleItem> list) {
		super(context, textViewResourceId, list);
		mContext= context;
		this.list = list;
		this.mInflater = LayoutInflater.from(context);
		width = Utils.getScreenWidth(mContext);
		imgParams = new RelativeLayout.LayoutParams(width/2, width/2);
		contactSize = (int) (width* 0.039);
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.cool_adapter_item, null);
			holder = new ViewHolder(convertView);
			holder.tvContact.setTextSize(TypedValue.COMPLEX_UNIT_PX, contactSize);
			holder.imgPhoto.setLayoutParams(imgParams);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		final CoolTitleItem ci = list.get(position);
		holder.tvContact.setText(ci.textpic);
//		String uri = "@drawable/"+ci.description;
//		setImg(uri, holder.imgPhoto);
		holder.imgPhoto.setImageResource(mContext.getResources().getIdentifier(ci.description, "drawable", mContext.getPackageName()));
		
		return convertView;
	}
	private void setImg(String name, ImageView img){
//		String pack = mContext.getPackageName();
//		int imgId = mContext.getResources().getIdentifier(name , null, mContext.getPackageName());
//		Drawable res = mContext.getResources().getDrawable(imgId);
//		img.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(),imgId));
//		img.setImageDrawable(res);
		img.setImageResource(mContext.getResources().getIdentifier("tet.png", "drawable", mContext.getPackageName()));
	}
	
	@Override
	public CoolTitleItem getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}
	
	
	private class ViewHolder{
		private TextView tvContact;
		private ImageView imgPhoto;
		public ViewHolder(View parent){
			tvContact = (TextView) parent.findViewById(R.id.contact_name);
			imgPhoto = (ImageView) parent.findViewById(R.id.img_contact);
		}
	}

}
